#!/bin/sh

set -e
set -x

BUILD_DIR=$1
TARGET_DIR=$2

# Remove the dbus daemon launch helper because once we've removed it makedevs
# will choke if it is still present in the device table.
sed -i '/dbus-daemon-launch-helper/d' $BUILD_DIR/_device_table.txt

# zsh is too fat to fit in our 32MB partition, so if we ever want to login
# again, root's login shell needs to change to something that isn't going to be
# deleted.
sed -i 's/usr\/bin\/zsh/bin\/dash/' $TARGET_DIR/etc/passwd

# TODO(cgibson): Removing any moca shared libraries or blobs seems to break
# external connectivity on RG200?
#
# ./etc/moca/moca20core-gen22.bin
# ./etc/moca/moca20core-gen21.bin
# ./etc/moca/moca20core-gen23.bin
# ./usr/lib/libmoca.so
# ./usr/lib/libmocacli.so
#
#####

FILES_TO_DELETE="./app
./bin/bsa2bluez
./bin/captive_portal
./bin/cpulog
./bin/dhcp-rogue
./bin/dnssd_hosts
./bin/gstatic
./bin/mcastreceive
./bin/memwatcher
./bin/sed
./chroot
./etc/avahi
./etc/dbus-1
./etc/diag.d
./etc/init.d/S03memwatcher
./etc/init.d/S04sysmgr
./etc/init.d/S05factoryreset
./etc/init.d/S11startupvideo
./etc/init.d/S30dbus
./etc/init.d/S41staticip
./etc/init.d/S45cmm
./etc/init.d/S48igmpproxy
./etc/init.d/S50avahi-daemon
./etc/init.d/S50waveguide
./etc/init.d/S50wifi
./etc/init.d/S55ssdpd
./etc/init.d/S60harddisk
./etc/init.d/S70inadyn
./etc/init.d/S75tlsdate
./etc/init.d/S80uitype
./etc/init.d/S80upnpd
./etc/init.d/S85catawampus
./etc/init.d/S90media
./etc/init.d/S91smb
./etc/init.d/S94cpulog
./etc/init.d/S94psstat
./etc/init.d/S94startupmods
./etc/init.d/S95marjoram
./etc/init.d/S95sageserver
./etc/init.d/S97basil
./etc/init.d/S98frobnicast
./etc/init.d/S99isoping
./etc/init.d/S99nicknamer
./etc/init.d/S99oregano
./etc/init.d/S99python_benchmark
./etc/init.d/S99readallfiles
./etc/init.d/S99stresstest
./etc/samba
./etc/ssl/misc
./etc/ssl/openssl.cnf
./etc/ssl/private
./etc/tlsdate
./home
./lib/ebtables
./lib/firmware
./lib/modules/3.2.26/backports
./sbin/tc
./tmp/run/dbus
./usr/bin/avahi-browse
./usr/bin/avahi-browse-domains
./usr/bin/avahi-publish
./usr/bin/avahi-publish-address
./usr/bin/avahi-publish-service
./usr/bin/avahi-resolve
./usr/bin/avahi-resolve-address
./usr/bin/avahi-resolve-host-name
./usr/bin/avahi-set-host-name
./usr/bin/burnin-wifi-setup
./usr/bin/bw_file_rd
./usr/bin/bw_mem
./usr/bin/bw_mmap_rd
./usr/bin/bw_pipe
./usr/bin/bw_tcp
./usr/bin/bw_unix
./usr/bin/bzdiff
./usr/bin/bzgrep
./usr/bin/bzip2
./usr/bin/bzip2recover
./usr/bin/cal
./usr/bin/chattr
./usr/bin/chfn
./usr/bin/chkdupexe
./usr/bin/chrt
./usr/bin/chsh
./usr/bin/cifsiostat
./usr/bin/clockadd
./usr/bin/clockspeed
./usr/bin/clockview
./usr/bin/cmm
./usr/bin/col
./usr/bin/colcrt
./usr/bin/colrm
./usr/bin/column
./usr/bin/crashme
./usr/bin/curl
./usr/bin/cytune
./usr/bin/dart
./usr/bin/dbus-cleanup-sockets
./usr/bin/dbus-daemon
./usr/bin/dbus-launch
./usr/bin/dbus-monitor
./usr/bin/dbus-run-session
./usr/bin/dbus-send
./usr/bin/dbus-uuidgen
./usr/bin/dhrystone
./usr/bin/disk
./usr/bin/easy_install
./usr/bin/easy_install-2.7
./usr/bin/enable-app-engine-project
./usr/bin/enough
./usr/bin/find
./usr/bin/find-servers
./usr/bin/flock
./usr/bin/flushdisk
./usr/bin/free
./usr/bin/frobclient
./usr/bin/fusermount
./usr/bin/gapplication
./usr/bin/gdb
./usr/bin/gdbus
./usr/bin/gdbus-codegen
./usr/bin/getfattr
./usr/bin/getopt
./usr/bin/gio-querymodules
./usr/bin/glib-compile-resources
./usr/bin/glib-compile-schemas
./usr/bin/gresource
./usr/bin/gsettings
./usr/bin/gunzip
./usr/bin/gzexe
./usr/bin/gzip
./usr/bin/hello
./usr/bin/hexdump
./usr/bin/hostapd_cli
./usr/bin/i2cdetect
./usr/bin/i2cdump
./usr/bin/i2cget
./usr/bin/i2cset
./usr/bin/input-repeat
./usr/bin/ionice
./usr/bin/iostat
./usr/bin/ipcmk
./usr/bin/ipcrm
./usr/bin/ipcs
./usr/bin/iperf
./usr/bin/iperf3
./usr/bin/isosize
./usr/bin/jpegtran
./usr/bin/lat_connect
./usr/bin/lat_ctx
./usr/bin/lat_fcntl
./usr/bin/lat_fifo
./usr/bin/lat_fs
./usr/bin/lat_http
./usr/bin/lat_mem_rd
./usr/bin/lat_mmap
./usr/bin/lat_ops
./usr/bin/lat_pagefault
./usr/bin/lat_pipe
./usr/bin/lat_proc
./usr/bin/lat_rpc
./usr/bin/lat_select
./usr/bin/lat_sem
./usr/bin/lat_sig
./usr/bin/lat_syscall
./usr/bin/lat_tcp
./usr/bin/lat_udp
./usr/bin/lat_unix
./usr/bin/lat_unix_connect
./usr/bin/less
./usr/bin/libcmm_sample
./usr/bin/libusb-config
./usr/bin/line
./usr/bin/link
./usr/bin/linux32
./usr/bin/linux64
./usr/bin/lmdd
./usr/bin/lmhttp
./usr/bin/log-delay
./usr/bin/upload-crash-log
./usr/bin/upload-crash-log2
./usr/bin/upload-logs
./usr/bin/upload-logs-and-wait
./usr/bin/upload-logs-loop
./usr/bin/upload-logs-now
./usr/bin/logger
./usr/bin/look
./usr/bin/loop_o
./usr/bin/lsattr
./usr/bin/lscpu
./usr/bin/lsmod
./usr/bin/lsusb
./usr/bin/lsusb.py
./usr/bin/mail-lock
./usr/bin/mail-touchlock
./usr/bin/mail-unlock
./usr/bin/mcookie
./usr/bin/memsize
./usr/bin/memstat
./usr/bin/memtester
./usr/bin/mhz
./usr/bin/minissdpd
./usr/bin/miniupnpd
./usr/bin/mpstat
./usr/bin/msleep
./usr/bin/namei
./usr/bin/nano
./usr/bin/netbios_hosts
./usr/bin/netperf
./usr/bin/netserver
./usr/bin/newgrp
./usr/bin/nfsiostat
./usr/bin/nmblookup
./usr/bin/openssl
./usr/bin/options.py
./usr/bin/par_mem
./usr/bin/par_ops
./usr/bin/pg
./usr/bin/pgrep
./usr/bin/pidstat
./usr/bin/pkg-config
./usr/bin/pkill
./usr/bin/pmap
./usr/bin/poweroff-with-message
./usr/bin/pwdx
./usr/bin/readallfiles
./usr/bin/reboot-if-fail
./usr/bin/renice
./usr/bin/rev
./usr/bin/rg-qa-settings
./usr/bin/rsync
./usr/bin/sadf
./usr/bin/sagesrv_dbg_cli
./usr/bin/sar
./usr/bin/script
./usr/bin/scriptreplay
./usr/bin/sdparm
./usr/bin/set-acs
./usr/bin/setarch
./usr/bin/setfattr
./usr/bin/setterm
./usr/bin/skill
./usr/bin/slabtop
./usr/bin/smbta-util
./usr/bin/snice
./usr/bin/sntpclock
./usr/bin/spin
./usr/bin/ssdk_sh
./usr/bin/sshfs
./usr/bin/stacktrace
./usr/bin/strace
./usr/bin/stream
./usr/bin/stress-disk
./usr/bin/stresstest
./usr/bin/taiclock
./usr/bin/taiclockd
./usr/bin/tailf
./usr/bin/tar
./usr/bin/taskset
./usr/bin/tdbrestore
./usr/bin/testusb
./usr/bin/timing_o
./usr/bin/tlb
./usr/bin/tload
./usr/bin/tlsdate
./usr/bin/tlsdate-dbus-announce
./usr/bin/tlsdate-helper
./usr/bin/tlsdate-routeup
./usr/bin/top
./usr/bin/tracepath
./usr/bin/tracepath6
./usr/bin/traceroute
./usr/bin/traceroute6
./usr/bin/ts_check
./usr/bin/ul
./usr/bin/uncompress
./usr/bin/update-acs-iptables
./usr/bin/upgradecheck
./usr/bin/usbhid-dump
./usr/bin/uuidgen
./usr/bin/w
./usr/bin/watch
./usr/bin/waveguide
./usr/bin/whereis
./usr/bin/whetstone
./usr/bin/wifiblaster
./usr/bin/zcat
./usr/bin/zcmp
./usr/bin/zdiff
./usr/bin/zegrep
./usr/bin/zfgrep
./usr/bin/zforce
./usr/bin/zgrep
./usr/bin/zless
./usr/bin/zmore
./usr/bin/znew
./usr/bin/zsh
./usr/bin/zsh-4.3.17
./usr/catawampus
./usr/lib/avahi
./usr/lib/libavahi-client.so
./usr/lib/libavahi-client.so.3
./usr/lib/libavahi-client.so.3.2.9
./usr/lib/libavahi-common.so
./usr/lib/libavahi-common.so.3
./usr/lib/libavahi-common.so.3.5.3
./usr/lib/libavahi-core.so
./usr/lib/libavahi-core.so.7
./usr/lib/libavahi-core.so.7.0.2
./usr/lib/libavahi-glib.so
./usr/lib/libavahi-glib.so.1
./usr/lib/libavahi-glib.so.1.0.2
./usr/lib/libavahi-gobject.so
./usr/lib/libavahi-gobject.so.0
./usr/lib/libavahi-gobject.so.0.0.4
./usr/lib/libcurl.so.4.3.0
./usr/lib/libdbus-1.so
./usr/lib/libdbus-1.so.3
./usr/lib/libdbus-1.so.3.8.11
./usr/lib/libgio-2.0.so.0.4200.2
./usr/lib/libgobject-2.0.so.0.4200.2
./usr/lib/libiperf.so.0.0.0
./usr/lib/libjpeg.so
./usr/lib/libjpeg.so.8
./usr/lib/libjpeg.so.8.4.0
./usr/lib/libncurses.so.5.7
./usr/lib/libnl-route-3.so.200.4.0
./usr/lib/libpcap.so.1.5.3
./usr/lib/libpng.so
./usr/lib/libpng15.so
./usr/lib/libpng15.so.15
./usr/lib/libpng15.so.15.18.0
./usr/lib/libprotobuf-lite.so
./usr/lib/libprotobuf-lite.so.10
./usr/lib/libprotobuf-lite.so.10.0.0
./usr/lib/libprotobuf.so
./usr/lib/libprotobuf.so.10
./usr/lib/libprotobuf.so.10.0.0
./usr/lib/libprotoc.so
./usr/lib/libprotoc.so.10
./usr/lib/libprotoc.so.10.0.0
./usr/lib/libtalloc.so.2.0.5
./usr/lib/libtiff.so
./usr/lib/libtiff.so.5
./usr/lib/libtiff.so.5.2.0
./usr/lib/libungif.so
./usr/lib/libungif.so.4
./usr/lib/libungif.so.4.1.4
./usr/lib/lowcase.dat
./usr/lib/media
./usr/lib/python2.7/calendar.pyo
./usr/lib/python2.7/email
./usr/lib/python2.7/imaplib.pyo
./usr/lib/python2.7/mailbox.pyo
./usr/lib/python2.7/multiprocessing
./usr/lib/python2.7/pydoc.pyo
./usr/lib/python2.7/site-packages/avahi
./usr/lib/python2.7/site-packages/google_api_python_client-1.0beta4-py2.7.egg
./usr/lib/python2.7/site-packages/qca83xx
./usr/lib/python2.7/unittest
./usr/lib/python2.7/wsgiref
./usr/lib/python2.7/xml
./usr/lib/tc
./usr/lib/upcase.dat
./usr/lib/valid.dat
./usr/lib/vfs
./usr/libexec
./usr/libexec/dbus-daemon-launch-helper
./usr/libexec/sftp-server
./usr/local
./usr/sbin/avahi-daemon
./usr/sbin/avahi-dnsconfd
./usr/sbin/badblocks
./usr/sbin/blkid
./usr/sbin/bonnie++
./usr/sbin/chroot
./usr/sbin/chvt
./usr/sbin/dmsetup
./usr/sbin/dnsmasq
./usr/sbin/e2freefrag
./usr/sbin/e2fsck
./usr/sbin/e2label
./usr/sbin/e2undo
./usr/sbin/fdformat
./usr/sbin/fdisk
./usr/sbin/filefrag
./usr/sbin/findfs
./usr/sbin/fsadm
./usr/sbin/hdparm
./usr/sbin/hostapd
./usr/sbin/hostapd
./usr/sbin/ifplugd
./usr/sbin/ifplugstatus
./usr/sbin/igmpproxy
./usr/sbin/inadyn
./usr/sbin/iw
./usr/sbin/ldattach
./usr/sbin/logsave
./usr/sbin/lvchange
./usr/sbin/lvconvert
./usr/sbin/lvcreate
./usr/sbin/lvdisplay
./usr/sbin/lvextend
./usr/sbin/lvm
./usr/sbin/lvmchange
./usr/sbin/lvmconf
./usr/sbin/lvmdiskscan
./usr/sbin/lvmdump
./usr/sbin/lvmsadc
./usr/sbin/lvmsar
./usr/sbin/lvreduce
./usr/sbin/lvremove
./usr/sbin/lvrename
./usr/sbin/lvresize
./usr/sbin/lvs
./usr/sbin/lvscan
./usr/sbin/modinfo
./usr/sbin/mtd_debug
./usr/sbin/mtdinfo
./usr/sbin/ndisc6
./usr/sbin/nfnl_osf
./usr/sbin/nl-class-add
./usr/sbin/nl-class-delete
./usr/sbin/nl-class-list
./usr/sbin/nl-classid-lookup
./usr/sbin/nl-cls-add
./usr/sbin/nl-cls-delete
./usr/sbin/nl-cls-list
./usr/sbin/nl-link-list
./usr/sbin/nl-pktloc-lookup
./usr/sbin/nl-qdisc-add
./usr/sbin/nl-qdisc-delete
./usr/sbin/nl-qdisc-list
./usr/sbin/nmbd
./usr/sbin/pvchange
./usr/sbin/pvck
./usr/sbin/pvcreate
./usr/sbin/pvdisplay
./usr/sbin/pvmove
./usr/sbin/pvremove
./usr/sbin/pvresize
./usr/sbin/pvs
./usr/sbin/pvscan
./usr/sbin/rdisc6
./usr/sbin/readprofile
./usr/sbin/readverity
./usr/sbin/rmmod
./usr/sbin/rtcwake
./usr/sbin/smartctl
./usr/sbin/smartctl.real
./usr/sbin/smbd
./usr/sbin/tcpdump
./usr/sbin/tlsdated
./usr/sbin/tune2fs
./usr/sbin/tunelp
./usr/sbin/usb_modeswitch
./usr/sbin/vgcfgbackup
./usr/sbin/vgcfgrestore
./usr/sbin/vgchange
./usr/sbin/vgck
./usr/sbin/vgconvert
./usr/sbin/vgcreate
./usr/sbin/vgdisplay
./usr/sbin/vgexport
./usr/sbin/vgextend
./usr/sbin/vgimport
./usr/sbin/vgimportclone
./usr/sbin/vgmerge
./usr/sbin/vgmknodes
./usr/sbin/vgreduce
./usr/sbin/vgremove
./usr/sbin/vgrename
./usr/sbin/vgs
./usr/sbin/vgscan
./usr/sbin/vgsplit
./usr/sbin/vigr
./usr/sbin/vipw
./usr/sbin/wpa_cli
./usr/sbin/wpa_passphrase
./usr/sbin/wpa_supplicant
./usr/sbin/zcav
./usr/share/LICENSES
./usr/share/applications
./usr/share/avahi
./usr/share/bash-completion
./usr/share/dbus-1
./usr/share/frob
./usr/share/gdb
./usr/share/getopt
./usr/share/glib-2.0
./usr/share/hwdata
./usr/share/locale/af
./usr/share/locale/am
./usr/share/locale/an
./usr/share/locale/ar
./usr/share/locale/as
./usr/share/locale/ast
./usr/share/locale/az
./usr/share/locale/be
./usr/share/locale/be@latin
./usr/share/locale/bg
./usr/share/locale/bn
./usr/share/locale/bn_IN
./usr/share/locale/bs
./usr/share/locale/ca
./usr/share/locale/ca@valencia
./usr/share/locale/cs
./usr/share/locale/cy
./usr/share/locale/da
./usr/share/locale/de
./usr/share/locale/dz
./usr/share/locale/el
./usr/share/locale/en@shaw
./usr/share/locale/en_AU
./usr/share/locale/en_CA
./usr/share/locale/en_GB
./usr/share/locale/en_NZ
./usr/share/locale/eo
./usr/share/locale/es
./usr/share/locale/et
./usr/share/locale/eu
./usr/share/locale/fa
./usr/share/locale/fi
./usr/share/locale/fo
./usr/share/locale/fr
./usr/share/locale/ga
./usr/share/locale/gl
./usr/share/locale/gu
./usr/share/locale/he
./usr/share/locale/hi
./usr/share/locale/hr
./usr/share/locale/hu
./usr/share/locale/hy
./usr/share/locale/id
./usr/share/locale/is
./usr/share/locale/it
./usr/share/locale/ja
./usr/share/locale/ka
./usr/share/locale/kk
./usr/share/locale/kn
./usr/share/locale/ko
./usr/share/locale/ku
./usr/share/locale/ky
./usr/share/locale/lt
./usr/share/locale/lv
./usr/share/locale/mai
./usr/share/locale/mg
./usr/share/locale/mk
./usr/share/locale/ml
./usr/share/locale/mn
./usr/share/locale/mr
./usr/share/locale/ms
./usr/share/locale/mt
./usr/share/locale/nb
./usr/share/locale/nds
./usr/share/locale/ne
./usr/share/locale/nl
./usr/share/locale/nn
./usr/share/locale/oc
./usr/share/locale/or
./usr/share/locale/pa
./usr/share/locale/pl
./usr/share/locale/ps
./usr/share/locale/pt
./usr/share/locale/pt_BR
./usr/share/locale/ro
./usr/share/locale/ru
./usr/share/locale/rw
./usr/share/locale/si
./usr/share/locale/sk
./usr/share/locale/sl
./usr/share/locale/sq
./usr/share/locale/sr
./usr/share/locale/sr@ije
./usr/share/locale/sr@latin
./usr/share/locale/sv
./usr/share/locale/ta
./usr/share/locale/te
./usr/share/locale/tg
./usr/share/locale/th
./usr/share/locale/tl
./usr/share/locale/tr
./usr/share/locale/tt
./usr/share/locale/ug
./usr/share/locale/uk
./usr/share/locale/vi
./usr/share/locale/wa
./usr/share/locale/xh
./usr/share/locale/yi
./usr/share/locale/zh_CN
./usr/share/locale/zh_HK
./usr/share/locale/zh_TW
./usr/share/pkgconfig
./usr/share/terminfo
./usr/share/usb.ids
./usr/sv
./usr/waveguide
./var/lib/dbus
./usr/wifi"

# Turn off execution trace before we actually hit the for loop, it's too noisy.
set +x
for file in $FILES_TO_DELETE; do
  rm -rf $TARGET_DIR/$file
done
